local fengdan = fk.CreateSkill {
  name = "lingling__fengdan",
}

Fk:loadTranslationTable{
  ["lingling__fengdan"] = "锋胆",
  [":lingling__fengdan"] = "你可以将【杀】当【闪】、【闪】当【杀】使用或打出并可以获得对方一张手牌。",

  ["#lingling__fengdan"] = "锋胆：你可以将【杀】当【闪】、【闪】当【杀】使用或打出",
  ["#lingling__fengdan-invoke"] = "锋胆：你可以获得 %dest 的一张手牌",
}

fengdan:addEffect("viewas", {
  pattern = "slash,jink",
  prompt = "#lingling__fengdan",
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    if #selected == 1 then return end
    local _c = Fk:getCardById(to_select)
    local c
    if _c.trueName == "slash" then
      c = Fk:cloneCard("jink")
    elseif _c.name == "jink" then
      c = Fk:cloneCard("slash")
    else
      return false
    end
    return (Fk.currentResponsePattern == nil and player:canUse(c)) or
      (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(c))
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return end
    local _c = Fk:getCardById(cards[1])
    local c
    if _c.trueName == "slash" then
      c = Fk:cloneCard("jink")
    elseif _c.name == "jink" then
      c = Fk:cloneCard("slash")
    end
    c.skillName = fengdan.name
    c:addSubcard(cards[1])
    return c
  end,
})

local spec = {
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    if room:askToSkillInvoke(player, {
      skill_name = fengdan.name,
      prompt = "#lingling__fengdan-invoke::"..to.id,
    }) then
      event:setCostData(self, {tos = {to}})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local card = room:askToChooseCard(player, {
      target = to,
      flag = "h",
      skill_name = fengdan.name,
    })
    room:obtainCard(player, card, false, fk.ReasonPrey, player, fengdan.name)
  end,
}

fengdan:addEffect(fk.CardUsing, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and not player.dead and table.contains(data.card.skillNames, fengdan.name) then
      local to
      if data.card.trueName == "slash" then
        to = data.tos[1]
      elseif data.card.name == "jink" then
        if data.responseToEvent then
          to = data.responseToEvent.from  -- jink
        end
      end
      if to and not to.dead and not to:isKongcheng() then
        event:setCostData(self, {tos = {to}})
        return true
      end
    end
  end,
  on_cost = spec.spec,
  on_use = spec.on_use,
})

fengdan:addEffect(fk.CardResponding, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if target == player and not player.dead and table.contains(data.card.skillNames, fengdan.name) then
      local to
      if data.responseToEvent then
        if data.responseToEvent.from == player then
          to = data.responseToEvent.to  -- duel used by zhaoyun
        else
          to = data.responseToEvent.from  -- savage_assault, archery_attack, passive duel

          -- TODO: Lenovo shu zhaoyun may fengdan liubei when responding to jijiang
        end
        if to and not to.dead and not to:isKongcheng() then
          event:setCostData(self, {tos = {to}})
          return true
        end
      end
    end
  end,
  on_cost = spec.spec,
  on_use = spec.on_use,
})

fengdan:addAI(nil, "vs_skill")

return fengdan
